//
//  LJHCateScrollView.m
//  HEARME
//
//  Created by liujinhui on 16/1/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LJHCateScrollView.h"

@interface LJHCateScrollView ()<UIScrollViewDelegate>

@end

@implementation LJHCateScrollView
- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        [self createSubViews];
    }
    
    return self;
}

- (void)createSubViews{
    
    
    //    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //    self.rightButton.frame = CGRectMake(CURRENTSCEEN.size.width - w, 0, w, SCREEN_HEIGHT*0.05);
    //    [self.rightButton setBackgroundImage:[UIImage imageNamed:@"reader_add_button@2x"] forState:UIControlStateNormal];
    //    [self addSubview:self.rightButton];
    
    self.isRight = NO;
    self.currentIndex = 0;
    self.btnArray = [NSMutableArray array];
    
    
    
    //按钮小scrollView
    self.titleScrollView = [[LJHBaseScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH*0.1)];
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    self.titleScrollView.bounces = NO;
    [self addSubview:self.titleScrollView];
    
    //跟着按钮走的线
    self.vernier = [[UIView alloc] initWithFrame:CGRectMake(WIDTH*0.03, self.titleScrollView.frame.size.height - 2, 50*(WIDTH/375), 2)];
    self.vernier.backgroundColor = [UIColor redColor];
    [self.titleScrollView addSubview:self.vernier];
    
    //下面装tableview的大scrollView
    self.bgScrollView = [[LJHBaseScrollView alloc] initWithFrame:CGRectMake(0, self.titleScrollView.frame.size.height, CURRENTSCEEN.size.width, CURRENTSCEEN.size.height - self.titleScrollView.frame.size.height)];
    self.bgScrollView.showsHorizontalScrollIndicator = NO;
    self.bgScrollView.bounces = NO;
    self.bgScrollView.delegate = self;
    self.bgScrollView.pagingEnabled = YES;
    [self addSubview:self.bgScrollView];
    
}



- (void)setBgScrollViewContents:(NSArray *)array{
    
    if (array.count != self.titleScrollView.subviews.count - 1) {
        NSLog(@"titleScrollView上的button个数与bgScrollview所对应的个数不等!!!");
    }
    
    for (int i = 0; i < array.count; i++) {
        UITableView *tableView = [array objectAtIndex:i];
        tableView.frame = CGRectMake(i * WIDTH, 0, WIDTH, HEIGHT-64);
        [self.bgScrollView addSubview:tableView];
//        self.bgScrollView = self;
    }
    
    self.bgScrollView.contentSize = CGSizeMake(WIDTH * array.count, 0);
    
}

- (void)btnAction:(UIButton *)btn{
    
    self.bgScrollView.contentOffset = CGPointMake(WIDTH * (btn.tag - 1000), 0);
    //    [btn setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
    [UIView animateWithDuration:0.5 animations:^{
        
        self.vernier.frame = CGRectMake(btn.frame.origin.x, self.titleScrollView.frame.size.height-2, btn.frame.size.width, 2);
        
        
        
    } completion:^(BOOL finished) {
        
    }];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    for (int i = 0; i<self.btnArray.count; i++) {
        UIButton *button = [self.btnArray objectAtIndex:i];
        if (button.tag!=btn.tag) {
            
            [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];

        }
    }
//    [self reloadInputViews];
    [self.myDelegate titleButtonAction:btn];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (self.currenX > scrollView.contentOffset.x) {
        self.isRight = NO;
    }else{
        self.isRight = YES;
    }
    
    [UIView animateWithDuration:0.1 animations:^{
        
        NSInteger index = scrollView.contentOffset.x / WIDTH;
        UIButton * button = (UIButton *)[self viewWithTag:index+1000];
        self.vernier.frame = CGRectMake(button.frame.origin.x, self.titleScrollView.frame.size.height-2, button.frame.size.width, 2);
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        
        for (int i = 0; i<self.btnArray.count; i++) {
            UIButton *btn = [self.btnArray objectAtIndex:i];
            if (btn.tag!=button.tag) {
                [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
                
            }
        }
        
//        [self reloadInputViews];
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:0.2 animations:^{
            
            NSInteger index = scrollView.contentOffset.x / WIDTH;
            CGPoint point = CGPointMake(0, 0);
            
            //让当前页是和标题页对应
            if (self.currentIndex != index) {
                self.currentIndex = index;
                
                [self.myDelegate endDraggingAction:self.currentIndex];
            }
            
            if (index >= 3 && index < self.btnArray.count - 2) {
                point.x = CURRENTSCEEN.size.width / 6 * (index - 2);
                self.titleScrollView.contentOffset = point;
            }else if (index < 3){
                self.titleScrollView.contentOffset = point;
            }
            
            if (self.titleScrollView.contentOffset.x <= 0) {
                point.x = 0;
                self.titleScrollView.contentOffset = point;
            }else if (self.titleScrollView.contentOffset.x >= self.titleScrollView.contentSize.width){
                point.x = self.titleScrollView.contentSize.width;
                self.titleScrollView.contentOffset = point;
            }
            
        }];
        
    }];
    
    self.currenX = scrollView.contentOffset.x;
}

#pragma mark - scrollView停止拖拽时
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    self.currenX = self.vernier.frame.origin.x;
    
    
}

-(void)setTitleContent:(NSArray *)array
{
    CGFloat btnWidth = 0.0;
    NSInteger i = 0;
    
    
    
    //根据传进来的数组创建按钮
    for (NSString *title in array) {
        
        LJHBaseButton *btn = [LJHBaseButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(WIDTH*0.03 + btnWidth, 0, 20, self.titleScrollView.frame.size.height);
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [btn sizeToFit];
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        btn.tag = 1000 + i;
        if (i==0) {
            
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            
        }
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleScrollView addSubview:btn];
        [self.btnArray addObject:btn];
        //        NSLog(@"%f",btn.frame.origin.x);
        
        btnWidth += btn.frame.size.width+WIDTH*0.03;
        i++;
        
    }
    
    self.titleScrollView.contentSize = CGSizeMake(btnWidth+WIDTH*0.03, 0);
    //    NSLog(@"==========%f",self.titleScrollView.contentSize.width);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
